package edu.pku.ss.taas.cloud.monitor.lightweight;

import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import edu.pku.ss.taas.cloud.monitor.lightweight.test.NetInterfaceData;

public class NetworkGather {
	private static Sigar sigar = SigarFactory.getSigar();
	private static Map networkInfo = new HashMap();
	NetInterfaceStat stat ;

	public  Map getNetworkInfo() {
        String name = MonitorUtil.getNetworkInterfaceName();
//		String name ="";
    	System.out.println("device name:"+name);
        long rxbps = 0;  
        long txbps = 0;  
        long speed = 0;
        double usage = -1.0 ;
        
        try {  
            if(stat == null){
            	stat = sigar.getNetInterfaceStat(name); 
            }
            speed = stat.getSpeed();
            
            if(speed<0)
            	speed = 1000*1000*100;
            
            System.out.println("name:"+name);
            long start = System.currentTimeMillis();  
            NetInterfaceStat statStart = sigar.getNetInterfaceStat(name);  
            long rxBytesStart = statStart.getRxBytes();  
            long txBytesStart = statStart.getTxBytes();
            
            System.out.println("rxBytesEnd:"+rxBytesStart);
            System.out.println("txBytesEnd:"+txBytesStart);
            
            Thread.sleep(2000);  
            long end = System.currentTimeMillis();  
            NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name);  
            long rxBytesEnd = statEnd.getRxBytes();  
            long txBytesEnd = statEnd.getTxBytes();  
            
            System.out.println("rxBytesEnd:"+rxBytesEnd);
            System.out.println("txBytesEnd:"+txBytesEnd);
            System.out.println("end - start:"+(end-start));
            rxbps = (rxBytesEnd - rxBytesStart)*1000/(end-start);  
            txbps = (txBytesEnd - txBytesStart)*1000/(end-start);  
            
            System.out.println("rxbps:"+rxbps);
            System.out.println("txbps:"+txbps);
            
            System.out.println("speed:"+speed);
            
            usage = (txbps+rxbps)*1000.0/speed;
            if(usage>100)
            	usage = usage/10;
            System.out.println("useage:"+usage);
           
        } catch (SigarException e) {  
              
        } catch (Exception e) {  
              
        }  
        
		networkInfo.put(Constants.SYS_NETWORK_SPEED, speed);
		networkInfo.put(Constants.SYS_NETWORK_USED_PERC, usage);
		
		System.out.println("speed:"+stat.getSpeed());
		
		return networkInfo;
	}

	/**
	 * @param args
	 * @throws SigarException
	 */
	public static void main(String[] args) throws SigarException {
//		System.out.println(sigar.getCpuInfoList()[0].toMap());
		
		System.out.println(new NetworkGather().getNetworkInfo());
	}

}
